package com.gsi.webstore.platform.storefront.pipelet.availability;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.xcs.capi.product.Product;
import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Storefront;

/**
 * 
 */
public class GetAvailability extends Pipelet
{
    /**
     * Constant used to access the pipeline dictionary with key 'Product'
     */
    public static final String DN_PRODUCT = "Product";
   
    /**
     * Constant used to access the pipeline dictionary with key 'StoreList'
     */
    public static final String DN_STORE_LIST = "StoreList";
    
    /**
     * Constant used to access the pipeline dictionary with key 'StoreAvailabilityMap'
     */
    public static final String DN_STORE_AVAILABILITY_MAP = "StoreAvailabilityMap";

    public static final String DN_QUANTITY = "Quantity";
    
    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    {        
        // lookup 'StoreList' in pipeline dictionary
        List<Storefront> storeList = (List<Storefront>)dict.get(DN_STORE_LIST);
        
        // lookup 'Product' in pipeline dictionary
        Product product = (Product)dict.get(DN_PRODUCT);

        Integer quantity = (Integer)dict.get(DN_QUANTITY);
        
        if (quantity == null) 
        {
            quantity = new Integer(1);
        }
        
        Map<String,AvailabilityIndicator> availabilityMap = new HashMap<String, AvailabilityIndicator>();
        
        for (Storefront sf:storeList) 
        {
            Map<String,AvailabilityIndicator> aiMap = getMap(String.valueOf(sf.getId()), quantity);
            
            AvailabilityIndicator ai = aiMap.get(product.getSKU());
            
            if (ai == null) 
            {
                ai = new AvailabilityIndicator(String.valueOf(sf.getId()), 0, 0, 0, quantity);
            }
            
            availabilityMap.put(sf.getStorefrontCode(), ai);
        }
        
        dict.put(DN_STORE_AVAILABILITY_MAP, availabilityMap);
        
        return PIPELET_NEXT;
    }
    
    private Map<String,AvailabilityIndicator> getMap(String storefrontCode, Integer quantity) 
    {
        Map<String,AvailabilityIndicator> ret = new HashMap<String, AvailabilityIndicator>();
        
        try
        {
            //load file of availability
            Properties props = new Properties();
            InputStream is = getClass().getClassLoader().getResourceAsStream(storefrontCode + ".availability");
            
            if (is!=null)
            {
                props.load(is);
            }
            
            for (Entry<Object,Object> entry: props.entrySet()) 
            {
                String sku = (String)entry.getKey();
                String value = (String)entry.getValue();
                String[] flags= value.split(",");                
                
                AvailabilityIndicator ai = 
                    new AvailabilityIndicator(storefrontCode, Integer.parseInt(flags[0]), Integer.parseInt(flags[1]), Integer.parseInt(flags[2]),quantity);
                
                ret.put(sku, ai);
            }
        }
        catch(IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return ret;
    }
}